using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;

namespace GameToolkit.Editor
{

    #region subpage : GUI style

    public class GUIStyleViewport : CheckWindow.SubPage
    {
        public GUIStyleViewport(CheckWindow window) : base("GUI Styles", window) { }

        GuiStyleTree mTree;

        public override void OnGUI(Rect rect)
        {
            if (mTree == null)
            {
                mTree = new GuiStyleTree(rect.width - 40f);
                mTree.Reload();
            }

            GUILayout.BeginVertical();
            //GUILayout.BeginHorizontal();
            //GUILayout.Label("单击示例将复制其名到剪贴板", "label");
            //GUILayout.FlexibleSpace();
            //GUILayout.Label("查找:");
            GUILayout.BeginArea(new Rect(rect.x + 5, rect.y, rect.width - 10, 20));
            mTree.searchString = QuickGUI.SearchTextBar(mTree.searchString ?? "");
            GUILayout.EndArea();
            //GUILayout.EndHorizontal();

            mTree.OnGUI(new Rect(5, rect.y + 30, rect.width - 10, rect.height - 30));

            //GUILayout.EndScrollView();
            GUILayout.EndVertical();
            SuperWindow.Repaint();
        }

        class GuiStyleTree : TreeView
        {
            public GuiStyleTree(float initWidth) : base(new TreeViewState())
            {
                MultiColumnHeaderState.Column[] cols = {
                new MultiColumnHeaderState.Column{headerContent = new GUIContent("Name") },
                new MultiColumnHeaderState.Column{headerContent = new GUIContent("Toggle On") },
                new MultiColumnHeaderState.Column{headerContent = new GUIContent("Toggle Off") },
                };
                var header = new MultiColumnHeaderState(cols);
                cols[0].width = initWidth * 0.35f;
                cols[1].width = initWidth * 0.28f;
                cols[2].width = initWidth * 0.28f;
                this.multiColumnHeader = new MultiColumnHeader(header);
            }

            protected override bool CanStartDrag(CanStartDragArgs args)
            {
                return true;
            }

            protected override void SetupDragAndDrop(SetupDragAndDropArgs args)
            {
                DragAndDrop.StartDrag("CheckView");
            }

            protected override DragAndDropVisualMode HandleDragAndDrop(DragAndDropArgs args)
            {
                if (args.performDrop)
                    Debug.LogFormat("Handle Drags {0} {1}", args.dragAndDropPosition, args.parentItem == null ? "nil" : args.parentItem.displayName);
                //GUI.Label(new Rect(Event.current.mousePosition, new Vector2(200, 40)),
                //    string.Format("Handle Drags {0} {1}", args.dragAndDropPosition, args.parentItem == null ? "nil" : args.parentItem.displayName), "box");
                return DragAndDropVisualMode.Move;
            }

            protected override TreeViewItem BuildRoot()
            {
                var root = new TreeViewItem(0, -1);
                var id = 1;
                foreach (GUIStyle style in GUI.skin)
                {
                    var it = new GuiStyleRow(id++, 0, style);
                    root.AddChild(it);
                }
                return root;
            }

            protected override float GetCustomRowHeight(int row, TreeViewItem item)
            {
                var it = item as GuiStyleRow;
                var style = it == null ? null : it.style;
                if (style != null && style.fixedHeight > 1)
                {
                    return Mathf.Max(25, style.fixedHeight + 6);
                }
                return 40;
            }

            protected override void RowGUI(RowGUIArgs args)
            {
                //base.RowGUI(args);
                var item = args.item as GuiStyleRow;
                var rect = args.GetCellRect(0);
                EditorGUI.SelectableLabel(new Rect(rect.x + 15f, rect.center.y - 10, rect.width - 15, 20), item.displayName);

                rect = args.GetCellRect(1);
                rect.height = item.style.fixedHeight > 1 ? item.style.fixedHeight : (rect.height - 6);
                rect.width -= 5f;
                rect.y += 3;
                GUI.Toggle(rect, true, item.displayName, item.style);

                rect = args.GetCellRect(2);
                rect.height = item.style.fixedHeight > 1 ? item.style.fixedHeight : (rect.height - 6);
                rect.width -= 5f;
                rect.y += 3;
                GUI.Toggle(rect, false, item.displayName, item.style);

                //rect = args.rowRect;
                //Handles.DrawLine(new Vector3(rect.xMin, rect.yMax), new Vector3(rect.xMax, rect.yMax));

            }
        }

        class GuiStyleRow : TreeViewItem
        {
            public GUIStyle style;

            public GuiStyleRow(int id, int deep, GUIStyle style) : base(id, deep, style.name)
            {
                this.style = style;
            }
        }
    }

    #endregion


}
